<?php
class View {

	/**
	 * Default templates extension
	 *
	 */
	const TPL_EXT = '.tpl.php';

	/**
	 * Template directory
	 *
	 */
	protected $_templateDir;
	
	/**
	 * Template varriables
	 *
	 * @var array
	 */
	protected $_templateVars = array();

	protected $_tplPath;
	
	//protected $_controller; 

	public function setTplDir($tplDir) {
		if(!is_dir($tplDir)) {
			die('�������� ����� ��������!');
		}
		$this->_templateDir = $tplDir;
	}

	public function assign($keyVar, $tplVar) {
		if(!isset($keyVar) && !isset($tplVar)) {
			die('����������� ����������� ���������� �������!');
		}
		$this->_templateVars[$keyVar] = $tplVar;
	}
	
	public function assignArray($params = array()) {
		foreach($params as $param => $parVal) {
			$this->assign($param, $parVal);
		}
	}
	
	protected function _getTemplate($tplName) {
		$this->_tplPath = $this->_templateDir . DIRECTORY_SEPARATOR . $tplName . self::TPL_EXT;
		if(!file_exists($this->_tplPath)) {
			die('������� '. $this->_tplPath . ' �� ����������!');
		}
	}

	public function render($tplName) {
		$this->_getTemplate($tplName);
		if(is_readable($this->_tplPath)) {
			foreach($this->_templateVars as $var => $value) {
				$$var = $value;
			}
			require_once $this->_tplPath;
		}
	}
}